import { _decorator, Component, instantiate, Label, Node, Prefab, Vec3 } from 'cc';
import { Global } from '../global';
const { ccclass, property } = _decorator;

@ccclass('buildingLv')
export class buildingLv extends Component {
    // 建筑物等级预制体
    @property(Prefab)
    buildingLv: Prefab = null;
    // 显示层
    @property(Node)
    showNode: Node = null;

    // 建筑物名称
    private bNameNode: Label = null;
    // 建筑物等级
    private bLvNode: Label = null;

    protected onLoad(): void {
        // 请求服务器的建筑物信息
        Global.http.sendRequest('/getBuildInfo', 'GET', null, (res) => {
            if (res.code == 0) {
                for (let i = 0; i < res.data.length; i++) {
                    const bname = res.data[i].buildName;
                    if (bname == '训练营') {
                        let bighouse = {
                            buildId: res.data[i].buildid,
                            buildName: res.data[i].buildName,
                            buildLv: res.data[i].buildLv,
                        }
                        Global.Buildings.push(bighouse); //将城堡信息存入全局变量
                    } else if (bname == '商城') {
                        let coast = {
                            buildId: res.data[i].buildid,
                            buildName: res.data[i].buildName,
                            buildLv: res.data[i].buildLv,
                        }
                        Global.Buildings.push(coast);
                    } else if (bname == '城堡') {
                        let beautyhouse = {
                            buildId: res.data[i].buildid,
                            buildName: res.data[i].buildName,
                            buildLv: res.data[i].buildLv,
                        }
                        Global.Buildings.push(beautyhouse);
                    } else if (bname == '铁匠铺') {
                        let ground = {
                            buildId: res.data[i].buildid,
                            buildName: res.data[i].buildName,
                            buildLv: res.data[i].buildLv,
                        }
                        Global.Buildings.push(ground);
                    } else if (bname == '兵营') {
                        let treehouse = {
                            buildId: res.data[i].buildid,
                            buildName: res.data[i].buildName,
                            buildLv: res.data[i].buildLv,
                        }
                        Global.Buildings.push(treehouse);
                    } else if (bname == '竞技场') {
                        let smalground = {
                            buildId: res.data[i].buildid,
                            buildName: res.data[i].buildName,
                            buildLv: res.data[i].buildLv,
                        }
                        Global.Buildings.push(smalground);
                    } else if (bname == '酒馆') {
                        let snowhouse = {
                            buildId: res.data[i].buildid,
                            buildName: res.data[i].buildName,
                            buildLv: res.data[i].buildLv,
                        }
                        Global.Buildings.push(snowhouse);
                    } else if (bname == '仓库') {
                        let smallhouse = {
                            buildId: res.data[i].buildid,
                            buildName: res.data[i].buildName,
                            buildLv: res.data[i].buildLv,
                        }
                        Global.Buildings.push(smallhouse);
                    }
                }
                this.setInfo();
            } else {
                console.log(res.msg)
            }
        })
    }

    // 设置信息
    setInfo() {
        const childNodes = this.node.children;
        for (let i = 0; i < childNodes.length; i++) {
            const child = childNodes[i];
            switch (child.name) {
                case 'coast':
                    this.showNameAndLv(child);
                    this.bNameNode.string = Global.Buildings[0].buildName;
                    this.bLvNode.string = Global.Buildings[0].buildLv + '';
                    break;
                case 'bighouse':
                    this.showNameAndLv(child);
                    this.bNameNode.string = Global.Buildings[1].buildName;
                    this.bLvNode.string = Global.Buildings[1].buildLv + '';
                    break;
                case 'beautyhouse':
                    this.showNameAndLv(child);
                    this.bNameNode.string = Global.Buildings[2].buildName;
                    this.bLvNode.string = Global.Buildings[2].buildLv + '';
                    break;
                case 'ground':
                    this.showNameAndLv(child);
                    this.bNameNode.string = Global.Buildings[3].buildName;
                    this.bLvNode.string = Global.Buildings[3].buildLv + '';
                    break;
                case 'treehouse':
                    this.showNameAndLv(child);
                    this.bNameNode.string = Global.Buildings[4].buildName;
                    this.bLvNode.string = Global.Buildings[4].buildLv + '';
                    break;
                case 'snowhouse':
                    this.showNameAndLv(child);
                    this.bNameNode.string = Global.Buildings[5].buildName;
                    this.bLvNode.string = Global.Buildings[5].buildLv + '';
                    break;
                case 'smalground':
                    this.showNameAndLv(child);
                    this.bNameNode.string = Global.Buildings[6].buildName;
                    this.bLvNode.string = Global.Buildings[6].buildLv + '';
                    break;
                case 'smallhouse':
                    this.showNameAndLv(child);
                    this.bNameNode.string = Global.Buildings[7].buildName;
                    this.bLvNode.string = Global.Buildings[7].buildLv + '';
                    break;
                default:
                    break;
            }
        }
    }

    showNameAndLv(child: Node) {
        let lvNode = instantiate(this.buildingLv); // 为建筑物创建一个预制体
        this.showNode.addChild(lvNode); // 将预制体添加到显示节点中
        this.bNameNode = lvNode.getChildByName('bname').getChildByName('buildingName').getComponent(Label); // 获取预制体中的建筑名称节点
        this.bLvNode = lvNode.getChildByName('lv').getChildByName('levelNum').getComponent(Label); // 获取预制体中的建筑等级节点
        lvNode.setPosition(child.position.x, child.position.y + 100, child.position.z); // 设置预制体的位置
    }
}


